from langdev_helper.util.search import search_with_bing, get_link_content
from langdev_helper.util.broswer import Broswer

from typing import Optional, Type

from langchain_core.callbacks import CallbackManagerForToolRun
from langchain_core.tools import BaseTool
from pydantic import BaseModel, Field

from langchain_community.tools.file_management.utils import (BaseFileToolMixin)


class KeywordInput(BaseModel):
    """Input for BingSearch."""

    keyword: str = Field(..., description="keyword to search")


class BingSearchTool(BaseFileToolMixin, BaseTool):  # type: ignore[override, override]
    """Tool that search web"""

    name: str = "bing_search"
    args_schema: Type[BaseModel] = KeywordInput
    description: str = "search web"

    def _run(
        self,
        keyword: str,
        run_manager: Optional[CallbackManagerForToolRun] = None,
    ) -> str:
        try:
            results = search_with_bing(keyword)

            Broswer.keyword = keyword
            Broswer.results = results

            return results
        except Exception as e:
            return "Error: " + str(e)
